本文以asp.net mvc
自己提供的身份验证为例
添加属性
在Models\IdentityModels.cs
里 ApplicationUser
类下添加你想添加的属性,比如生日
1 | public DateTime BirthDate { get; set; } |
使用Migrations修改数据库
由于已经改变了model的属性所以应该修改之前的数据库
前往工具/NUGET/程序包管理器控制台
输入
Enable-Migrations
初始化迁移Add-Migration "Birthdate"
添加名为Birthdate的迁移文件进你的工程Update-Database
执行你的迁移文件更新数据库
修改你的view对应的model
在 Models\AccountViewModels.cs
中找到RegisterViewModel
类添加属性,当然也可以添加你想要的特性
1 | public DateTime BirthDate { get; set; } |
修改对应的view展示
1 | <div class="input-field col s12 m3 l3"> |
运行就能看到修改后的结果了
如何展示新的信息
得到
UserId
, 可以通过ASP.NET Identity system
1
var currentUserId = User.Identity.GetUserId();
实例化
UserManager
在ASP.Identity system命名空间下1
var manager = new UserManager<MyUser>(new UserStore<MyUser>(new MyDbContext()));
得到当前用户实例
1
var currentUser = manager.FindById(User.Identity.GetUserId());
通过实例展现修改的信息
1
var birthdate = currentUser.BirthDate
自己创建新的model,展示想显示的信息
1 | public class MyUser : IdentityUser |
Getting Profile information
When the User Logs in, you can display the profile information by doing the following
Get the current logged in UserId, so you can look the user up in ASP.NET Identity system
- var currentUserId = User.Identity.GetUserId();
Instantiate the UserManager in ASP.Identity system so you can look up the user in the system
- var manager = new UserManager
(new UserStore (new MyDbContext()));
- var manager = new UserManager
Get the User object
- var currentUser = manager.FindById(User.Identity.GetUserId());
Get the profile information about the user
- currentUser.MyUserInfo.FirstName